﻿//Ajax调用
/*
url:要访问的链接   参数类型:String
paramMap:参数列表  参数类型:Map
callBack:回调接口  参数类型:js对象
注:callBack对象类一定要有onSuccess(ResultBean rb)方法和onFail(ResultBean rb)方法
*/
function AjaxGet(url, paramet, callBack) {
    jQuery.support.cors = true;
    $.ajax({
        url: url + encodeURIComponent(paramet),
        type: 'get',
        dataType: "json",
        data: "",
        success: function (json) {
            if (json == null) {
                Alert("服务器返回数据失败");
                return;
            }
            var jsonData = json;
            if (json.Code == jsonCode.success && json.Data != null && json.Data != '') {
                var jsonData = JSON.parse(json.Data);
            }
            callBack(jsonData);
        },
        error: function (rs) {

            ("网络错误!");
        }
    });
}
//post请求
function AjaxPost(url, paramet, callBack) {
    jQuery.support.cors = true;
    $.ajax({
        url: url,
        type: 'post',
        dataType: "json",
        data: encodeURI(paramet),
        success: function (json) {
            if (json == null) {
                Alert("服务器返回数据失败");
                return;
            }
            var jsonData = json;
            if (json.Code == jsonCode.success && json.Data != null && json.Data != '') {
                jsonData = JSON.parse(json.Data);
            }
            callBack(jsonData);
        },
        error: function (rs) {
            Alert("网络错误!");
        }
    });
}

function checkCode(data) {
    //json操作失败
    if (data == null || data.Code != jsonCode.success)
        return false;
    return true;
}

function emptyData(data) {
    //json空集合，或空数据
    if (data.Data == null || data.Data.length < 1)
        return false;
    return true;
}

//URL参数
function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = decodeURI(window.location.search).substr(1).match(reg);
    if (r != null)
        return unescape(r[2]);
    return "";
}

function toUrl(url) {
    window.location.hef = url;
}


//基本信息
function getStoreInfo() {
    var url = siteUrl.fotmatUrl(siteUrl.GetInfo);
    AjaxGet(url, "", function (data) {
        if (!checkCode(data)) {
            $(".sitelogo").removeClass("hide");
            Alert(data.Message);    //显示错误消息
            return;
        }
        if (!emptyData(data))
            return; //显示空数据提示

        var info = data.Data;
        $(".sitelogo").attr("src", info.SiteLogo);
        $(".sitelogo").removeClass("hide");
        setTimeout(function () {
            $(".store-address").html(info.Address);
            $(".store-phone").html(info.Phone);
            $(".store-mobile").html(info.Mobile);
            $(".store-fax").html(info.Fax);
            $(".store-qq").html(info.QQ);
            $(".store-email").html(info.Email);
            if (info.WeApp_Qrcode != '') {
                var WeApp_Qrcode = siteConfig.imgPath.WeApp.replace("{storeid}", storeInfo.storeId) + info.WeApp_Qrcode;
                $(".contact-content-mid-item-top img").attr('src', WeApp_Qrcode);
            }

            if (info.WeService_Qrcode != '') {
                var WeService_Qrcode = siteConfig.imgPath.WeService.replace("{storeid}", storeInfo.storeId) + info.WeService_Qrcode;
                $(".contact-content-mid-item-bottom img").attr('src', WeService_Qrcode);
            }
        }, 200);
    });
}

    //导航条
function GetNavList(index) {
        AjaxGet(siteUrl.NavWidgetList, "", function (data) {
        if (!checkCode(data)) {
            Alert(data.Message);    //显示错误消息
            return;
        }
        if (!emptyData(data))
            return; //显示空数据提示
    
        var html = "<div class='navbar-rel-item hot' style='margin-left:0px;' onclick=" + "location.href='/pages/index.html?storeid=" + storeInfo.storeId + "'" + "><span>首页</span></div>";
        var list = data.Data.Config;
        for (var i = 0; i < list.length; i++) {
            var item = list[i];
            html += getNavItem(item);
    }
        $(".pc_index_nav").html(html);
        $(document).ready(function () {
            $('.navbar-rel-item').click(function (event) {
                $('.navbar-rel div').removeClass("hot")
                $(this).addClass("hot")
            });
            return false ; 
        });
        setNavIndex(index);	//选中导航
        });
}

function getNavItem(item) {
    var html = "";
    var linkType = item.LinkType;
    var LinkValue = eval(item.LinkValue);

    if (linkType == "store_class") {
        //店铺分类
        html = "<div class='navbar-rel-item' data-linktype='" + linkType + "'><a href='/pages/product.html?storeid=" + storeInfo.storeId + "&linktype=" + LinkValue.LinkType + "&linkid=" + LinkValue.Id + "'><span>" + item.ShowName + "</span></a></div>";
    }
    else if (linkType == "product") {
        //商品详情
        html = "<div class='navbar-rel-item' data-linktype='" + linkType + "'><a href='/pages/product_item.html?storeid=" + storeInfo.storeId + "&linktype=" + LinkValue.LinkType + "&pid=" + LinkValue.Id + "' ><span>" + item.ShowName + "</span></a></div>";
    }
    if (linkType == "news_type") {
        //内容分类
        html = "<div class='navbar-rel-item' data-linktype='" + linkType + "'><a href='/pages/news.html?storeid=" + storeInfo.storeId + "&linktype=" + LinkValue.LinkType + "&linkid=" + LinkValue.Id + "'><span>" + item.ShowName + "</span></a></div>";
    }
    else if (linkType == "news") {
        //新闻详情
        html = "<div class='navbar-rel-item' data-linktype='" + linkType + "'><a href='/pages/news_item.html?storeid=" + storeInfo.storeId + "&linktype=" + LinkValue.LinkType + "&newsid=" + LinkValue.Id + "'><span>" + item.ShowName + "</span></a></div>";
    }
    else if (linkType == "h5page") {
        //h5链接
        html = "<div class='navbar-rel-item'\" data-linktype='" + linkType + "'><a href='" + LinkValue.Url + "'><span>" + item.ShowName + "</span></a></div>";
    } else if (linkType == "page") {
        //页面
        if (LinkValue.Name == '关于我们')
            html = "<div class='navbar-rel-item' data-linktype='" + linkType + "'><a href='/pages/about.html?storeid=" + storeInfo.storeId + "&linktype=" + LinkValue.LinkType + "&newsid=" + LinkValue.Id + "'><span>" + item.ShowName + "</span></a></div>";
        else
            html = "<div class='navbar-rel-item' data-linktype='" + linkType + "'><a href='/pages/news_item.html?storeid=" + storeInfo.storeId + "&linktype=" + LinkValue.LinkType + "&newsid=" + LinkValue.Id + "'><span>" + item.ShowName + "</span></a></div>";
    }
    return html;
}


    //根据linktype类型，获取url
    function getLinkUrl(link) {
        var url = "";
        var id = link.Id;
        var linkUrl = link.Url;
        var linkType = link.LinkType;

        if (linkType == "store_class") {
            //店铺分类
            url = siteUrl.htmlUrl.ProductList.replace("{storeid}", storeInfo.storeId).replace("{linkid}", id);
        }
        else if (linkType == "product") {
            //商品详情
            url = siteUrl.htmlUrl.ProductDetail.replace("{storeid}", storeInfo.storeId).replace("{pid}", id);
        }
        else if (linkType == "news_type") {
            //内容列表
            url = siteUrl.htmlUrl.NewsList.replace("{storeid}", storeInfo.storeId).replace("{linkid}", id);
        }
        else if (linkType == "news") {
            //内容详情
            url = siteUrl.htmlUrl.NewsDetail.replace("{storeid}", storeInfo.storeId).replace("{newsid}", id);
        }
        else if (linkType == "h5page") {
            //h5外链
            url = linkUrl;
        }
        else if (linkType == 'page' && link.Name == '关于我们') {
            url = "/pages/about.html?storeid=" + storeInfo.storeId + "&linktype=" + linkType + "&newsid=" +id;
    }
        return url;
}

    //选中导航
    function setNavIndex(index) {
        //index = index == null ? 0 : index;
        //$(".navbar-rel-item").removeClass("hot");
        //$(".navbar-rel-item").eq(index).addClass("hot");
}

    function Alert(msg) {
        layer.msg(msg)
}

    //提交留言
    function submitForm() {
        var myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
        if (document.getElementById("input1").value == "") {
            layer.msg('请输入您的姓名');
        }
        else if (document.getElementById("input2").value == "") {
            layer.msg('请输入您的电话号码');
        } else if (!myreg.test($('#input2').val())) {
            layer.msg('你输入的手机号格式不正确。');
        }
        else if (document.getElementById("input3").value == "") {
            layer.msg('请输入您的工作单位名称');
        }
        else if (document.getElementById("input4").value == "") {
            layer.msg('请输入您的留言');
        }
        else {
            validate();
    }
}
    //提交留言
    function validate() {
        layer.confirm('您确定要提交信息吗？', {
                btn: ['是的', '取消'] //按钮
        }, function () {
            subUserMsg();
        });
}
    //提交留言
    function subUserMsg() {
        var fullname = $('#input1').val();
        var phone = $('#input2').val();
        var companyname = $('#input3').val();
        var discription = $('#input4').val();
        var url = siteUrl.fotmatUrl(siteUrl.AddConsult);
        //提交用户联系信息
        AjaxGet(url, "&companyname=" + companyname + "&fullname=" + fullname + "&phone=" + phone + "&valicode=123&discription=" +discription, function (data) {
            if (!checkCode(data)) {
                layer.msg(data.Message, { icon: 2 });    //显示错误消息
                return;
        }
            if (!emptyData(data)) {
                return; //显示空数据
        }
            if (data.Data == "success") {
                layer.msg("信息提交完成，我们尽快与您联系", { icon: 1 });
                successclear();
            } else if (data.Data == "sub") {
                successclear();
                layer.msg("信息提交完成，我们尽快与您联系", { icon: 1 });
            } else {
                layer.msg("网络超时，请稍后再试！", { icon: 2 });
            }
        });
}
    function returnFloat(value) {
        var value = Math.round(parseFloat(value) * 100) / 100;
        var xsd = value.toString().split(".");
        if (xsd.length == 1) {
            value = value.toString() + ".00";
            return value;
        }
        if (xsd.length > 1) {
            if (xsd[1].length < 2) {
                value = value.toString() + "0";
            }
            return value;
        }
    }

    //成功清空记录
    function successclear() {
        $('#input1').val("");
        $('#input2').val("");
        $('#input3').val("");
        $('#input4').val("");
    }